package yqt.com.cnki.service.impl;

import haha.life.common.CutPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yqt.com.cnki.bo.QBKJBo;
import yqt.com.cnki.dao.IQBKJDao;
import yqt.com.cnki.po.QBKJPo;
import yqt.com.cnki.service.IQBKJService;

import java.util.List;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/12/28 14:20
 * @Version 1.0
 */
@Service
public class QBKJServiceImpl implements IQBKJService {
    @Autowired
    private IQBKJDao qbkjDao;

    @Override
    public CutPageBean<QBKJPo> complexSearchQBKJ(QBKJBo qbkjBo) {
        qbkjBo.setStart(qbkjBo.getPage(), qbkjBo.getPageSize());
        String sortStr = this.getSortStr(qbkjBo);
        qbkjBo.setSortArrStr(sortStr);
        CutPageBean<QBKJPo> cutPage = new CutPageBean<>();
        List<QBKJPo> list = qbkjDao.complexSearchQBKJ_List(qbkjBo);
        int count = (int) qbkjDao.complexSearchQBKJ_Count(qbkjBo);
        cutPage.initCutPage(count, qbkjBo.getPageSize(), list);
        return cutPage;
    }

    @Override
    public QBKJPo findQBKJById(String sysId) {
        return qbkjDao.findQBKJById(sysId);
    }

    private String getSortStr(QBKJBo qbkjBo) {
        String sortArrStr = "";
        StringBuffer sb = new StringBuffer();
        if (qbkjBo != null) {
            String[] sortArr = qbkjBo.getSortArr();
            if (sortArr != null || sortArr.length != 0) {
                for (String s : sortArr) {
                    sb.append("分类 = ").append(s).append(" or ");
                }
                if (sb.length() > 3) {
                    sortArrStr = sb.substring(0, sb.length() - 3);
                }
            }
        }
        return sortArrStr;
    }
}
